/**
 * @Author: ls <ls-mac>
 * @Date:   2019-08-08 15:38:55
 * @Last modified by:   ls-mac
 * @Last modified time: 2019-11-05 21:47:05
 */

import {
  servicePage,
  serviceList,
  serviceDelete,
  serviceDetail,
  serviceAdd,
  serviceEdit,
  serviceDetailDelete,
  serviceRelation,
} from 'services/service';

export default {
  namespace: 'serviceList',
  state: {
    data: {
      list: [],
      pagination: {},
    },
    addServiceDetailList: [],
    detailData: {},
    serviceList: [],
    serviceRelation: [],
  },

  effects: {
    *fetch({ payload, callback }, { call, put }) {
      const response = yield call(servicePage, payload);
      yield put({
        type: 'saveList',
        payload: response,
      });
    },

    // 查询套餐服务list
    *getServiceList({ payload, callback }, { call, put }) {
      const response = yield call(serviceList, payload);
      yield put({
        type: 'saveGetServiceList',
        payload: response,
      });
      if (callback) callback(response);
    },

    *serviceRelation({ payload, callback }, { call, put }) {
      const response = yield call(serviceRelation, payload);
      yield put({
        type: 'saveServiceRelation',
        payload: response,
      });
      if (callback) callback(response);
    },

    *clearServiceRelation({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveClearServiceRelation',
      });
    },

    *deleteDetailData({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveDeleteDetailData',
      });
      if (callback) callback();
    },

    *addServiceDetailList({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveAddServiceDetailList',
        payload: payload,
      });
    },

    *deleteServiceList({ payload, callback }, { call, put }) {
      if (payload.id) {
        const response = yield call(serviceDetailDelete, payload);
      }
      yield put({
        type: 'saveDeleteServiceList',
        payload: payload,
      });
    },

    *clearServiceList({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveClearServiceList',
      });
    },

    *doAddServiceDetailList({ payload, callback }, { call, put }) {
      const response = yield call(extraItemAdd, payload);
      if (callback) callback(response);
    },

    *serviceEdit({ payload, callback }, { call, put }) {
      const response = yield call(serviceEdit, payload);
      if (callback) callback(response);
    },

    *serviceAdd({ payload, callback }, { call, put }) {
      const response = yield call(serviceAdd, payload);
      if (callback) callback(response);
    },

    // 新增套餐服务--修改
    *editServiceList({ payload }, { call, put }) {
      yield put({
        type: 'saveServiceList',
        payload,
      });
    },

    *serviceDetail({ payload, callback }, { call, put }) {
      const response = yield call(serviceDetail, payload);
      yield put({
        type: 'saveServiceDetail',
        payload: response,
      });
    },

    *serviceDelete({ payload, callback }, { call, put }) {
      const response = yield call(serviceDelete, payload);
      if (callback) callback(response);
    },

    // 修改套餐服务明细图片
    *editImg({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveEditImg',
        payload,
      });
    },
  },

  reducers: {
    saveList(state, action) {
      const {
        data: { serviceList, pageIndex, pageSize, recordCount },
      } = action.payload;

      const newList = serviceList.map(v => {
        v.key = `k${v.id}`;
        return v;
      });
      return {
        ...state,
        data: {
          list: newList,
          pagination: {
            current: pageIndex,
            total: recordCount,
            pageSize,
          },
        },
      };
    },

    saveDeleteDetailData(state) {
      return {
        ...state,
        detailData: {},
      };
    },

    saveAddServiceDetailList(state, action) {
      const { addServiceDetailList } = state;
      addServiceDetailList.push({
        key: addServiceDetailList.length + 1,
        type: 'add',
        description: '',
        name: '',
        price: '',
      });
      return {
        ...state,
        addServiceDetailList,
      };
    },

    saveDeleteServiceList(state, action) {
      const { addServiceDetailList } = state;
      const { key } = action.payload;
      const newList = addServiceDetailList.filter(v => {
        return v.key != key;
      });

      return {
        ...state,
        addServiceDetailList: newList,
      };
    },

    saveClearServiceList(state, action) {
      return {
        ...state,
        addServiceDetailList: [],
      };
    },

    saveServiceList(state, action) {
      const { addServiceDetailList } = state;
      const newList = addServiceDetailList.map(val => {
        const v = val;
        let v1 = v;
        if (v.key === action.payload.key) {
          v1 = { ...v, ...action.payload };
        }
        return v1;
      });
      return {
        ...state,
        addServiceDetailList: newList,
      };
    },

    saveServiceDetail(state, action) {
      const { data } = action.payload;

      if (data.serviceDetailList) {
        data.serviceDetailList = data.serviceDetailList.map((v, i) => {
          v.key = i;
          return v;
        });
      } else {
        data.serviceDetailList = [];
      }

      return {
        ...state,
        detailData: data,
        addServiceDetailList: data.serviceDetailList,
      };
    },

    saveEditImg(state, action) {
      const { addServiceDetailList } = state;
      addServiceDetailList.map(v => {
        if (v.key == action.payload.key) {
          v.attachmentUrl = action.payload.attachmentUrl;
        }
      });
      return {
        ...state,
        addServiceDetailList: [...addServiceDetailList],
      };
    },
    saveGetServiceList(state, action) {
      const { data } = action.payload;

      return {
        ...state,
        serviceList: data,
      };
    },

    saveServiceRelation(state, action) {
      const { data } = action.payload;
      return {
        ...state,
        serviceRelation: data ? [...data] : [],
      };
    },
    saveClearServiceRelation(state, action) {
      return {
        ...state,
        serviceRelation: [],
      };
    },
  },
};
